﻿Public Class FMGuru

    'member variables
    Private _cGuru As CGuru
    Private _dView As DataView

    'forms
    Private WithEvents _ftbGuru As FTbGuru
    Private WithEvents _frguru As FRGuru

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        'Me.Text = My.Settings.NamaAplikasi & " [Master Guru]"
        _cGuru = New CGuru
    End Sub

    #Region "SettinganAwal"
    '-----------------------------------------------------------------------------------------------
    Private IsFormBeingDragged As Boolean = False
    Private MouseDownX As Integer
    Private MouseDownY As Integer

    Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles LabelJudul.MouseDown, PanelJudul.MouseDown, MyBase.MouseDown
        If e.Button = MouseButtons.Left Then
            IsFormBeingDragged = True
            MouseDownX = e.X
            MouseDownY = e.Y
        End If
    End Sub

    Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) Handles MyBase.MouseUp, LabelJudul.MouseUp, PanelJudul.MouseUp
        If e.Button = MouseButtons.Left Then
            IsFormBeingDragged = False
        End If
    End Sub

    Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles MyBase.MouseMove, LabelJudul.MouseMove, PanelJudul.MouseMove
        If IsFormBeingDragged Then
            Dim temp As Point = New Point()

            temp.X = Me.Location.X + (e.X - MouseDownX)
            temp.Y = Me.Location.Y + (e.Y - MouseDownY)
            Me.Location = temp
            temp = Nothing
        End If
    End Sub

    Private Sub Panel1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint, PanelJudul.Paint
        Dim p As New Pen(Color.FromArgb(213, 251, 185), 2)
        'set text box border
        PaintTextBox(Me, e.Graphics, p)

        p.Dispose()
    End Sub

    Private Sub PaintTextBox(cont As Control, g As Graphics, p As Pen)
        For Each ctl As Control In cont.Controls
            If ctl.Visible Then
                p.Color = ctl.BackColor
                If (TypeOf ctl Is TextBox) Or (TypeOf ctl Is ComboBox) Or (TypeOf ctl Is PictureBox) Then
                    g.DrawRectangle(p, New Rectangle(ctl.Location + New Size(1, 1), ctl.Size - New Size(2, 2)))
                ElseIf TypeOf ctl Is Panel Then
                    PaintTextBox(ctl, g, p)
                ElseIf TypeOf ctl Is GroupBox Then
                    PaintTextBox(ctl, g, p)
                End If
            End If

        Next
    End Sub

    Private Sub SetRegionTextBox(cont As Control)
        For Each ctl As Control In cont.Controls
            If (TypeOf ctl Is TextBox) Or (TypeOf ctl Is ComboBox) Or (TypeOf ctl Is PictureBox) Then
                If ctl.Region Is Nothing Then
                    ctl.Region = New Region(New Rectangle(2, 2, ctl.Width - 4, ctl.Height - 4))
                Else
                    ctl.Region = Nothing
                End If
            ElseIf TypeOf ctl Is Panel Then
                SetRegionTextBox(ctl)
            ElseIf TypeOf ctl Is GroupBox Then
                SetRegionTextBox(ctl)
            End If
        Next
    End Sub

    Private Sub btnMaximize_Click(sender As System.Object, e As System.EventArgs) Handles btnMaximize.Click

        WindowState = FormWindowState.Maximized
        btnMinimize.Visible = False
        btnMaximize.Visible = False
        btnClose.Visible = False
    End Sub

    Private Sub btnMinimize_Click(sender As System.Object, e As System.EventArgs) Handles btnMinimize.Click
        WindowState = FormWindowState.Minimized
    End Sub

    Private Sub btnClose_Click(sender As System.Object, e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub FSizeChanged(sender As Object, e As System.EventArgs) Handles Me.SizeChanged
        If WindowState <> FormWindowState.Maximized Then
            btnMinimize.Visible = True
            btnMaximize.Visible = True
            btnClose.Visible = True
        Else
            btnMinimize.Visible = False
            btnMaximize.Visible = False
            btnClose.Visible = False
        End If
    End Sub
    '-----------------------------------------------------------------------------------------------
#End Region

    Private Sub KeluarToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles KeluarToolStripMenuItem.Click
        Me.Close()
        'FadingForm()
    End Sub


    Private Sub FMGuru_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'set text box border
        SetRegionTextBox(Me)

        'menampilkan data ke datagrid
        Try
            TampilkanData()
        Catch ex As Exception
            Me.Close()
        End Try


        ''set text box border
        'If tbFilter.Region Is Nothing Then
        '    tbFilter.Region = New Region(New Rectangle(2, 2, tbFilter.Width - 4, tbFilter.Height - 4))
        'Else
        '    tbFilter.Region = Nothing
        'End If

        'set text box border
        SetRegionTextBox(Me)
    End Sub

    Private Sub TampilkanData()
        'menampilkan data ke dalam datagridview
        Try
            _dView = _cGuru.getAll.DefaultView
            Me.DGMaster.DataSource = _dView
        Catch ex As Exception
            If ex.Message.Contains("Unable to connect") Then
                MessageBox.Show("Gagal konek pusat data", "Peringatan", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                Throw ex
            End If
        End Try

    End Sub

    Private Sub TambahToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TambahToolStripMenuItem.Click
        Dim fcov As New FCover
        fcov.Show()
        'menampilkan form tambah guru
        _ftbGuru = New FTbGuru
        _ftbGuru.ShowDialog()
        fcov.Close()

    End Sub

    Private Sub _ftbGuru_DataTersimpan() Handles _ftbGuru.DataTersimpan
        'refresh data setelah tambah data
        TampilkanData()
    End Sub

    Private Sub DGMaster_RowPostPaint(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowPostPaintEventArgs) Handles DGMaster.RowPostPaint
        'menampilkan nomor urut pada datagridview
        DGMaster.Rows(e.RowIndex).Cells("CNo").Value = e.RowIndex + 1
    End Sub


    ''' <summary>
    ''' Menampilkan form rubah guru
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub RubahToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RubahToolStripMenuItem.Click
        'form cover
        Dim fcov As New FCover
        fcov.Show()
        'menampilkan form rubah guru
        _frguru = New FRGuru(DGMaster.CurrentRow.Cells("CID").Value, DGMaster.CurrentRow.Cells("CNip").Value, DGMaster.CurrentRow.Cells("CNama").Value)
        _frguru.ShowDialog()
        fcov.Close()
    End Sub

    Private Sub _frguru_DataTerupdate() Handles _frguru.DataTerupdate
        'refresh data setelah update
        TampilkanData()
    End Sub

    Private Sub ToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DetailToolStripMenuItem.Click
        'fcover
        Dim fcov As New FCover
        fcov.Show()

        Dim _fdGuru As New FDGuru(DGMaster.CurrentRow.Cells("CID").Value, DGMaster.CurrentRow.Cells("CNama").Value)
        _fdGuru.ShowDialog()

        fcov.Close()
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        TambahToolStripMenuItem.PerformClick()
    End Sub

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        RubahToolStripMenuItem.PerformClick()
    End Sub

    Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
        'HapusToolStripMenuItem.PerformClick()


    End Sub

    Private Sub Button4_Click(sender As System.Object, e As System.EventArgs) Handles btnDetilKonsentrasi.Click
        DetailToolStripMenuItem.PerformClick()
    End Sub

    Private Sub Button5_Click(sender As System.Object, e As System.EventArgs) Handles btnKeluar.Click
        KeluarToolStripMenuItem.PerformClick()
    End Sub


    Private Sub KkTextBox1_TextChanged_1(sender As System.Object, e As System.EventArgs) Handles tbFilter.TextChanged
        'filter by nama
        Try
            _dView.RowFilter = cbFilter.Text.Trim & " like '%" & tbFilter.Text.Trim & "%'"
        Catch ex As Exception

        End Try

    End Sub


    Private Sub Button8_Click(sender As System.Object, e As System.EventArgs) Handles btnClose.Click
        KeluarToolStripMenuItem.PerformClick()
    End Sub

    
End Class